// ECharts composable
export const useECharts = () => {
  const { $echarts } = useNuxtApp();

  // 创建图表实例
  const createChart = (container: HTMLElement, options?: any) => {
    const chart = $echarts.init(container);
    if (options) {
      chart.setOption(options);
    }
    return chart;
  };

  // 销毁图表
  const disposeChart = (chart: any) => {
    if (chart && !chart.isDisposed()) {
      chart.dispose();
    }
  };

  // 调整图表大小
  const resizeChart = (chart: any) => {
    if (chart && !chart.isDisposed()) {
      chart.resize();
    }
  };

  // 设置图表选项
  const setChartOption = (chart: any, options: any, notMerge?: boolean) => {
    if (chart && !chart.isDisposed()) {
      chart.setOption(options, notMerge);
    }
  };

  return {
    echarts: $echarts,
    createChart,
    disposeChart,
    resizeChart,
    setChartOption,
  };
};
